namespace EyeSoft.Domain
{
	using System;
	using System.ComponentModel;
	using System.Linq.Expressions;

	using EyeSoft.Reflection;

	public abstract class Entity<T> : IEntity, INotifyPropertyChanged where T : IComparable<T>
	{
		public virtual event PropertyChangedEventHandler PropertyChanged;

		public virtual T Id { get; set; }

		IComparable IEntity.Id
		{
			get { return (IComparable)Id; }
			set { Id = (T)value; }
		}

		protected virtual void OnPropertyChanged<TType, TProperty>(Expression<Func<TType, TProperty>> expression)
		{
			var propertyName = expression.PropertyName();

			OnPropertyChanged(propertyName);
		}

		protected virtual void OnPropertyChanged(string propertyName)
		{
			var handler = PropertyChanged;

			if (handler != null)
			{
				handler(this, new PropertyChangedEventArgs(propertyName));
			}
		}
	}
}